package services

import config.{Global, MyAwait, MyFile, MyRow, Search}
import dao.GenomeDao
import javax.inject.{Inject, Singleton}
import org.apache.commons.io.FileUtils
import utils.TableUtils

import scala.jdk.CollectionConverters._
import scala.concurrent.ExecutionContext

@Singleton
class OnStart @Inject()(genomeDao: GenomeDao)(implicit exec: ExecutionContext) extends MyFile with MyRow with MyAwait {

  val genome = genomeDao.getAllInfo.toAwait

  TableUtils.genome = genome.groupBy(_.species).map(x =>
    x._1 -> x._2.map(_.getMapByT()))


  TableUtils.geneIdToID = genome.map { x =>
    x.geneid -> x.id
  }.toMap


  Search.search = genome.map { x =>
    (x.id, x.species, Seq(x.geneid, x.nrname, x.swissname, x.cog, x.go, x.ko, x.pfam).mkString("\t"))
    // (x.id, x.species, Seq(x.geneid, x.go  , x.pfam).mkString("\t"))
  }

  TableUtils.fpkm = s"${Global.path}/config/fpkm.txt".readLines.tail.map { x =>
    val line = x.split("\t")
    val geneid = line.head.trim
    geneid -> line.tail.mkString("\t")
  }.toMap

  TableUtils.fpkmHeader = s"${Global.path}/config/fpkm_header.txt".readFileToString.split("\t").map(_.trim)


  /*  val all = genomeDao.getAllInfo.toAwait

    all.groupBy(_.species).foreach { x =>
      val species = x._1
      val go = x._2.filter(_.go != "_").map(y => y.geneid + "\t" + y.go.split(";").map(_.split("::").head).distinct.mkString(";"))
      val ko = x._2.filter(_.ko != "_").map(y => y.geneid + "\t" + y.ko.split(";").map(_.split("::").head).distinct.head)
      val chr = x._2.map(_.chr).distinct
      val geneid = x._2.map(_.geneid)

      FileUtils.writeLines(s"${Global.path}\\enrich/${species}_ko.xls".toFile, ("#Query\tKO ID" +: ko).toBuffer.asJava)
      FileUtils.writeLines(s"${Global.path}\\enrich/${species}_go.xls".toFile, go.toBuffer.asJava)
      FileUtils.writeLines(s"${Global.path}\\blastData/${species}_chr.txt".toFile, chr.toBuffer.asJava)
      FileUtils.writeLines(s"${Global.path}\\blastData/${species}_geneid.txt".toFile, geneid.toBuffer.asJava)

    }*/


  val rnaRows = s"${Global.path}/RNA-seq".listFile.map { x =>
    val name = x.getName.split('.').head
    val lines = x.readLines
    val head = lines.head.split("\t")
    (name, lines.tail, head)
  }

  val nameMap = Map("MsG0180" -> "Cole01G", "MsG0280" -> "Cole02G", "MsG0380" -> "Cole03G", "MsG0480" -> "Cole04G",
    "MsG0580" -> "Cole05G", "MsG0680" -> "Cole06G", "MsG0780" -> "Cole07G", "MsG0880" -> "Cole08G")

  TableUtils.rnaRows = rnaRows.map { x =>
    x._1 -> x._2.map { y =>
      y.split("\t").zipWithIndex.map { z =>
        z._2.toString -> z._1
      }.toMap
    }.toSeq
  }.toMap

  TableUtils.rnaHeader = rnaRows.map(x => x._1 -> (Array("Treatment") ++: x._3.tail)).toMap
  TableUtils.rna = rnaRows.flatMap { x =>
    x._2.map(_.split("\t")).map { y =>
      val rnaId = y.head
      val n = y.head.take(7)
      val r = nameMap.getOrElse(n,n)
      val name = r + y.head.split('.').head.drop(7)

      val geneId = y.head.split('.').init.mkString(".")
      (name, rnaId, x._1, y.tail)
    }
  }.groupBy(_._1).map { case (geneId, info) =>
    geneId -> info.groupBy(_._2).map { case (rnaId, info) =>
      rnaId -> info.map(x => (x._3, x._4.map(_.toDouble)))
    }
  }


  def rename = {

    val nameMap = Map("MsG0180" -> "Cole01G", "MsG0280" -> "Cole02G", "MsG0380" -> "Cole03G", "MsG0480" -> "Cole04G",
      "MsG0580" -> "Cole05G", "MsG0680" -> "Cole06G", "MsG0780" -> "Cole07G", "MsG0880" -> "Cole08G")


    "H:\\南农油茶数据库\\2024_4_8/range".listFile.foreach { x =>

      val n = x.getName.take(7)
      val r = nameMap.get(n)
      if (r.nonEmpty) {
        val name = r.get + x.getName.split('.').head.drop(7) + ".txt"
        println(name)
        val l = x.readFileToString.replaceAll(n, r.get)
        FileUtils.writeStringToFile(s"H:\\南农油茶数据库\\2024_4_8/new/range/$name".toFile, l)
      }

    }

  }

}
